package com.ruoyi.project.travel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.RandomNumberUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.travel.domain.VipCode;
import com.ruoyi.project.travel.service.IVipCodeService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * VIP码Controller
 *
 * @author HYD
 * @date 2020-06-14
 */
@RestController
@RequestMapping("/travel/code" )
public class VipCodeController extends BaseController {
    @Autowired
    private IVipCodeService vipCodeService;

    /**
     * 查询VIP码列表
     */
    @PreAuthorize("@ss.hasPermi('travel:code:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(VipCode vipCode) {
        startPage();
        QueryWrapper<VipCode> queryWrapper = new QueryWrapper<VipCode>();
        if(vipCode.getUserId() != null){
            queryWrapper.like("user_id",vipCode.getUserId());
        }
        if(vipCode.getEndTime() != null){
            queryWrapper.eq("end_time",vipCode.getEndTime());
        }
        List<VipCode> list = vipCodeService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出VIP码列表
     */
    @PreAuthorize("@ss.hasPermi('travel:code:export')" )
    @Log(title = "VIP码" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(VipCode vipCode) {
        QueryWrapper<VipCode> queryWrapper = new QueryWrapper<VipCode>(vipCode);
        List<VipCode> list = vipCodeService.list(queryWrapper);
        ExcelUtil<VipCode> util = new ExcelUtil<VipCode>(VipCode. class);
        return util.exportExcel(list, "code" );
    }

    /**
     * 获取VIP码详细信息
     */
    @PreAuthorize("@ss.hasPermi('travel:code:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(vipCodeService.getById(id));
    }

    /**
     * 新增VIP码
     */
    @PreAuthorize("@ss.hasPermi('travel:code:add')" )
    @Log(title = "VIP码" , businessType = BusinessType.INSERT)
    @GetMapping
    public AjaxResult add(Integer number,Date endTime) {
        if(number > 0){
            int index = 0;
            try {
                for(int i=0;i<number;i++){
                    VipCode vipCode = new VipCode();
                    vipCode.setCode(RandomNumberUtils.GetKey(10));
                    vipCode.setEndTime(endTime);
                    vipCode.setCreateTime(new Date());
                    if(vipCodeService.save(vipCode)) index++;
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return AjaxResult.success("共生成激活码 " + index + "个");
        }
        return AjaxResult.error("数量需要大于零");
    }

    /**
     * 修改VIP码
     */
    @PreAuthorize("@ss.hasPermi('travel:code:edit')" )
    @Log(title = "VIP码" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VipCode vipCode) {
        return toAjax(vipCodeService.updateById(vipCode) ? 1 : 0);
    }

    /**
     * 删除VIP码
     */
    @PreAuthorize("@ss.hasPermi('travel:code:remove')" )
    @Log(title = "VIP码" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(vipCodeService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
